package com.example.music.service.Impl;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.music.dao.ConsumerMapper;
import com.example.music.dto.ConsumerDto;
import com.example.music.dto.ConsumerSexDto;
import com.example.music.entity.Consumer;
import com.example.music.entity.Singer;
import com.example.music.entity.Song;
import com.example.music.service.ConsumerService;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ContentValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ConsumerServiceImpl extends BaseServiceImpl<Consumer,ConsumerMapper> implements ConsumerService {
    @Autowired
    ConsumerMapper consumerMapper;

    @Override
    protected ConsumerMapper getMapper() {
        return consumerMapper;
    }

    @Override
    public Consumer login(ConsumerDto consumerDto) {
        List<Consumer> consumers = consumerMapper.selectList(null);
        for (Consumer consumer: consumers){
            if (consumer.getUsername().equals(consumerDto.getUsername())
                    && consumer.getPassword().equals(consumerDto.getPassword())){
                return consumer;
            }
        }
        return null;
    }

    @Override
    public AjaxJson updateConsumerUrl(Integer id, MultipartFile file) {
        if (file.isEmpty()){
            return AjaxJson.getError();
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = ContentValue.userDir+ContentValue.Slash+"avatorImages";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        File dest = new File(filePath+ContentValue.Slash+fileName);
        String mysqlDir = ContentValue.Slash+"avatorImages"+ContentValue.Slash+fileName;
//        String mysqlDir = "/img/songPic/"+fileName;
        try {
            file.transferTo(dest);
            Consumer song = new Consumer();
            song.setId(id.longValue());
            song.setAvator(mysqlDir);
            Integer update = consumerMapper.updateById(song);
            if (update == 1){
                return AjaxJson.getSuccess();
            }
            return AjaxJson.getError();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxJson sex() {
        List<Consumer> consumers = consumerMapper.selectList(null);
        Map<Byte, List<Consumer>> collect = consumers.parallelStream().collect(Collectors.groupingBy(Consumer::getSex));
        ConsumerSexDto consumerSexDto1 = new ConsumerSexDto();
        ConsumerSexDto consumerSexDto0 = new ConsumerSexDto();
        consumerSexDto1.setSex("男");
        consumerSexDto0.setSex("女");
        List<Consumer> consumers1 = collect.get((byte)1);
        List<Consumer> consumers0 = collect.get((byte)0);
        consumerSexDto1.setNum(consumers1.size());
        consumerSexDto0.setNum(consumers0.size());
        ArrayList<ConsumerSexDto> list = new ArrayList<>();
        list.add(consumerSexDto0);
        list.add(consumerSexDto1);
        return AjaxJson.getSuccessData(list);
    }

    public Integer update(Consumer consumer) {
        return consumerMapper.updateById(consumer);
    }

    @Override
    public AjaxJson updateSingerPic(Integer id, MultipartFile file) {
        if (file.isEmpty()){
            return AjaxJson.getError();
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = ContentValue.userDir+ContentValue.Slash+"avatorImages";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        File dest = new File(filePath+ContentValue.Slash+fileName);
        String mysqlDir = "/avatorImages/"+fileName;
        try {
            file.transferTo(dest);
            Consumer singer = new Consumer();
            singer.setId(id.longValue());
            singer.setAvator(mysqlDir);
            Integer update = update(singer);
            if (update == 1){
                System.out.println(mysqlDir);
                return AjaxJson.getSuccess(mysqlDir);
            }
            return AjaxJson.getError();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
